package com.lian.pet.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lian.pet.common.basic.enums.DataTypeEnum;
import com.lian.pet.common.cache.redis.service.RedisService;
import com.lian.pet.domain.dto.AddPetFeedinglogsDTO;
import com.lian.pet.domain.dto.QueryPetFeedinglogsDTO;
import com.lian.pet.domain.entity.PetFeedinglogs;
import com.lian.pet.domain.vo.PetFeedinglogsVO;
import com.lian.pet.factory.ResponseBeanFactory;
import com.lian.pet.mapper.PetFeedinglogsMapper;
import com.lian.pet.service.PetFeedinglogsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class PetFeedinglogsServiceImpl implements PetFeedinglogsService {
    private final PetFeedinglogsMapper petFeedinglogsMapper;
    private final RedisService redisService;

    @Override
    public void addPetFeedinglogs(AddPetFeedinglogsDTO req){
        PetFeedinglogs petFeedinglogs = ResponseBeanFactory.getPetFeedinglogs(req);
        petFeedinglogsMapper.insert(petFeedinglogs);
        // 添加数据时 删除Cache数据
        redisService.delKey(DataTypeEnum.PETFEEDINGLOGS.name());
        log.info("执行成功[新增宠物饲养记录]");
    }

    @Override
    public List<PetFeedinglogsVO> queryPetFeedinglogs(QueryPetFeedinglogsDTO req){
        IPage<PetFeedinglogs> page = new Page<>(req.getPageNum(),req.getPageSize());
        IPage<PetFeedinglogs> iPage = petFeedinglogsMapper.selectPage(page, Wrappers.<PetFeedinglogs>lambdaQuery()
                .eq(PetFeedinglogs::getPetId, req.getPetId())
                .orderByDesc(PetFeedinglogs::getFeedingTime));
        List<PetFeedinglogs> petFeedinglogs = iPage.getRecords();
        if(petFeedinglogs.isEmpty()){
            return Collections.emptyList();
        }
        List<PetFeedinglogsVO> petFeedinglogsVOS = new ArrayList<>();
        petFeedinglogs.forEach(petFeedinglog -> {
            PetFeedinglogsVO petFeedinglogsVO = PetFeedinglogsVO.fromPetFeedinglogs(petFeedinglog);
            petFeedinglogsVOS.add(petFeedinglogsVO);
        });

        log.info("执行成功[查询宠物饲养记录列表]");
        return petFeedinglogsVOS;
    }

    @Override
    public PetFeedinglogsVO getPetFeedinglogs(Integer id){
        PetFeedinglogs petFeedinglogs = petFeedinglogsMapper.selectById(id);
        return PetFeedinglogsVO.fromPetFeedinglogs(petFeedinglogs);
    }

    @Override
    public void deletePetFeedinglogs(Integer id){
        petFeedinglogsMapper.deleteById(id);
    }

    @Override
    public void updatePetFeedinglogs(Integer id, AddPetFeedinglogsDTO req) {
        PetFeedinglogs existingPetFeedinglogs = petFeedinglogsMapper.selectById(id);
        if (existingPetFeedinglogs == null) {
            // 如果找不到对应的宠物饲养信息，则抛出异常或者返回错误信息
            // 这里我选择抛出一个运行时异常示例
            throw new RuntimeException("宠物饲养信息不存在");
        }

        existingPetFeedinglogs.setFood(req.getFood());
        existingPetFeedinglogs.setAmount(req.getAmount());
        existingPetFeedinglogs.setFeedingTime(req.getFeedingTime());
        existingPetFeedinglogs.setCreateTime(new Date());

        petFeedinglogsMapper.updateById(existingPetFeedinglogs);

        // 更新数据时 删除Cache数据
        redisService.delKey(DataTypeEnum.PETFEEDINGLOGS.name());

        log.info("执行成功[修改宠物饲养信息]");
    }

}
